//
//  ChooseVoucherCell.m
//  DaoZiJinFu
//
//  Created by woker on 16/6/24.
//  Copyright © 2016年 iwoker. All rights reserved.
//

#import "ChooseVoucherCell.h"

@implementation ChooseVoucherCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    ChooseVoucherCell *cell = [tableView dequeueReusableCellWithIdentifier:CHOOSEVOUCHER_CELL];
    if (!cell) {
        cell = [[ChooseVoucherCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CHOOSEVOUCHER_CELL];
    }
    return cell;
}

- (void)setUpCellWithModel:(VoucherModel *)model {
    _voucherModel = model;
    
    _voucherLimitDay.text = [NSString stringWithFormat:@"适用于%.0lf个月及以上理财产品",[_voucherModel.limit_day doubleValue]/30.0];
    
    _deadlineLab.text = [NSString stringWithFormat:@"有效期至%@",_voucherModel.expire_time];
    _voucherFrom.text = _voucherModel.source_title;
    _useCondition.text = _voucherModel.name;
    
    NSString *imgName = @"";
    UIColor *white = [UIColor whiteColor];
    switch ([model.status integerValue]) {
        case 1:imgName = @"可用券";_voucherValue.textColor = white;break;
        case 2:imgName = @"可用券";_voucherValue.textColor = white;break;
        case 3:imgName = @"已使用";_voucherValue.textColor = white;break;
        default:imgName = @"已过期";_voucherValue.textColor = white;break;
    }
    _backgroundImg.image = [UIImage imageNamed:imgName];
    
    NSString *valurStr = [NSString stringWithFormat:@"%ld",[_voucherModel.amount integerValue]];
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@元",valurStr]];
    NSDictionary *amountAttr = @{NSFontAttributeName:[UIFont systemFontOfSize:30.0f],NSForegroundColorAttributeName:([_voucherModel.status integerValue] == 1 ? white : white)};
    NSDictionary *yuanAttr = @{NSFontAttributeName:[UIFont systemFontOfSize:15.0f],NSForegroundColorAttributeName:([_voucherModel.status integerValue] == 1 ? white : white)};
    [attrStr setAttributes:amountAttr range:NSMakeRange(0, valurStr.length)];
    [attrStr setAttributes:yuanAttr range:NSMakeRange(valurStr.length, 1)];
    _voucherValue.attributedText = [[NSAttributedString alloc] initWithAttributedString:attrStr];
    
    if ([_voucherModel.status integerValue] == 1) {
        _voucherFrom.textColor = white;
    } else {
        _voucherFrom.textColor = white;
    }
}

@end
